package com.mengchengnet.chaperonage.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.mengchengnet.chaperonage.R;
import com.mengchengnet.chaperonage.domain.base.Comment;
import com.mengchengnet.chaperonage.util.ViewUtil;
import com.mengchengnet.chaperonage.view.CircleImageView;
import com.mengchengnet.chaperonage.view.RatingBar;

import java.util.List;

/**
 * Created by Administrator on 2017/8/20.
 */

public class CommentAdapter extends BaseAdapter{
    private LayoutInflater mInflater = null;
    private List<Comment> list;
    private Context mContext;
    public CommentAdapter(Context context, List<Comment> lv)
    {
        //根据context上下文加载布局，这里的是Demo17Activity本身，即this
        this.mInflater = LayoutInflater.from(context);
        list=lv;
        mContext=context;
    }
    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int i) {
        return list.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup viewGroup) {
        ViewHolder holder = null;
        //如果缓存convertView为空，则需要创建View
        if(convertView == null)
        {
            holder = new ViewHolder();
            //根据自定义的Item布局加载布局
            convertView = mInflater.inflate(R.layout.item_comment, null);
            holder.headIcon= (CircleImageView) convertView.findViewById(R.id.item_comment_headIcon);
            holder.name= (TextView) convertView.findViewById(R.id.item_comment_name);
            holder.time= (TextView) convertView.findViewById(R.id.item_comment_time);
            holder.content= (TextView) convertView.findViewById(R.id.item_comment_content);
            holder.ratingBar= (RatingBar) convertView.findViewById(R.id.item_comment_rating);
            //将设置好的布局保存到缓存中，并将其设置在Tag里，以便后面方便取出Tag
            convertView.setTag(holder);
        }else {
            holder = (ViewHolder)convertView.getTag();
        }

        ViewUtil.showImageView(mContext,holder.headIcon,list.get(position).getHeadIcon());
        holder.name.setText(list.get(position).getName());
        holder.time.setText(list.get(position).getTime());
        holder.content.setText(list.get(position).getContent());
        holder.ratingBar.setStar(Integer.parseInt(list.get(position).getStar()));

        return convertView;
    }

    //ViewHolder静态类
    static class ViewHolder
    {
        public CircleImageView headIcon;
        public TextView name;
        public TextView time;
        public TextView content;
        public RatingBar ratingBar;
    }
}
